<template>
  <div class="upload-file">
    <div class="file-upload">
      <div class="upload-box">
        <div class="file-name" v-if="fileName">
          <div class="name-box">
            <span>{{ fileName }}</span>
            <IconFont iconType="icon-close_icon" @click="fileName = ''" customClass="close-icon" />
          </div>
          <div v-if="fileProcess > 0 && fileProcess < 100" class="file-process">{{ fileProcess }}%</div>
        </div>
        <a-upload
          :showUploadList="false"
          accept=".xls, .xlsx"
          :disabled="loading"
          :customRequest="handleCustomRequest"
          :beforeUpload="handleBeforeUpload"
        >
          <div class="box-upload">
            <IconFont iconType="icon-shangchuan_icon" customClass="upload-icon" />
            <span>{{ fileName ? '重新上传' : '文件上传' }}</span>
          </div>
        </a-upload>
      </div>
      <div class="download-box" @click="downFile">
        <IconFont iconType="icon-a-CombinedShapefuzhi" customClass="box-icon" />
        <div class="box-name">下载文件模板</div>
      </div>
    </div>
    <div class="file-info">请上传xls、xlsx后缀的Excel文件</div>
  </div>
</template>
<script lang="ts" setup>
import type { PropType } from 'vue'
import { ref, shallowRef, onMounted, computed } from 'vue'
import { message } from 'ant-design-vue'
import { getUploadFile } from '@/utils/axios/request'
import { downloadFile } from '@/utils'
import IconFont from '@/components/IconFont/index.vue'
import { CloseOutlined, UploadOutlined, DownloadOutlined } from '@ant-design/icons-vue'

const emit = defineEmits(['operateSuccess'])

const loading = ref(false)
const fileName = ref('')
const fileProcess = ref(0)

const handleBeforeUpload = (file: any) => {
  const fileType = file.type
  const validFileTypes = [
    'application/vnd.ms-excel',
    'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet'
  ]

  if (!validFileTypes.includes(fileType)) {
    message.error('只允许上传 Excel 文件')
    return false
  }

  return true
}
const countDownTime = () => {
  const interval = setTimeout(() => {
    if (fileProcess.value >= 99) {
      clearTimeout(interval)
      return
    } else {
      countDownTime()
    }
    fileProcess.value += 9 //进度条进度
  }, 80)
}

const handleCustomRequest = async (options: any) => {
  fileProcess.value = 0
  countDownTime()
  // 示例：使用 axios 进行文件上传
  const formData = new FormData()
  formData.append('file', options.file)
  fileName.value = options.file.name
  try {
    loading.value = true
    let res = await getUploadFile(formData, '/api/tenant/manager/batch/detail/enterprise/uploadTopEnterpriseExcel')
    fileProcess.value = 100
    message.success('文件上传成功')
    emit('operateSuccess')
    loading.value = false
  } catch (error) {
    fileProcess.value = 100
    loading.value = false
  }
}

// 文件下载
const downFile = () => {
  downloadFile(import.meta.env.VITE_COMPANY_ADDRESS)
}
</script>
<style lang="less" scoped>
.upload-file {
  .file-upload {
    display: flex;
    align-items: center;
    .upload-box {
      height: 32px;
      background: #ffffff;
      border-radius: 2px;
      border: 1px solid #b6b6b6;
      padding: 6px 0;
      display: flex;
      align-items: center;
      &:hover {
        border-color: @primary-color;
      }
      .file-name {
        display: flex;
        align-items: center;
        padding: 0 8px 0 4px;
        border-right: 1px solid #b6b6b6;
        .name-box {
          padding: 2px 8px;
          font-size: 14px;
          color: #192236;
          line-height: 20px;
          background: #e0e1e6;
          margin-right: 30px;
          display: flex;
          align-items: center;
          .close-icon {
            color: #535d71;
            font-size: 12px;
            margin-left: 8px;
            cursor: pointer;
            &:hover {
              color: @primary-color;
            }
          }
        }
        .file-process {
          color: @primary-color;
          line-height: 14px;
          font-size: 12px;
        }
      }
      .box-upload {
        padding: 0 12px;
        color: #535d71;
        line-height: 32px;
        font-size: 14px;
        display: flex;
        align-items: center;
        cursor: pointer;
        &:hover {
          color: @primary-color;
        }
        span {
          margin-left: 4px;
        }
      }
    }
    .download-box {
      color: @primary-color;
      font-size: 14px;
      line-height: 20px;
      margin-left: 16px;
      display: flex;
      align-items: center;
      cursor: pointer;
      &:hover {
        color: #6bb070;
      }
      .box-icon {
        font-weight: 600;
        margin-right: 4px;
      }
    }
  }
  .file-info {
    margin-top: 4px;
    font-size: 12px;
    color: #c0c0c0;
  }
}
</style>
